using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using ZeroconfService;

using SmokeAlarm;

namespace SmokeAlarm
{
    public partial class DummyForm : Form 
    {
        public DummyForm(SmokeSettings settings)
        {
            InitializeComponent();
            m_settings = settings;
			this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
			this.ShowInTaskbar = false;
			this.Show();
			m_Browser = new Browser(this);
			m_Browser.Show();
			m_Browser.StartBrowsing();
        }

        NetService resolving = null;
		public void Resolve(NetService resolve)
		{
			if (resolving != null)
			{
				resolving.Stop();
			}

			resolve.DidUpdateTXT += new NetService.ServiceTXTUpdated(resolve_DidUpdateTXT);
			resolve.DidResolveService += new NetService.ServiceResolved(resolve_DidResolveService);
			resolve.ResolveWithTimeout(10); /* FIXME timeout doesn't work */
		}

		public void resolve_DidResolveService(NetService service)
        {
			ConnectTo((IPEndPoint)service.Addresses[0]);
        }

		public void resolve_DidUpdateTXT(NetService service)
		{

		}

		private void ConnectTo(IPEndPoint ipe)
		{
			try
			{
				m_client = new SmokeClient(ipe);
			    m_client.SetCallback(this);
				m_Browser.Close();
			}
			catch (Exception e)
			{
				MessageBox.Show("Connection to server failed: " + e.Message, "Could not connect to server");
				throw;
			}
		}

		public static string InputBox(string prompt, string title, string defaultValue)
		{
			InputBoxDialog ib = new InputBoxDialog();
			ib.FormPrompt = prompt;
			ib.FormCaption = title;
			ib.DefaultValue = defaultValue;
			ib.ShowDialog();
			string s = ib.InputResponse;
			ib.Close();
			return s;
		}

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Smoke();
        }

        private void smokeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Smoke();
        }

        private void awayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_settings.isAway = !m_settings.isAway;
            awayToolStripMenuItem.Checked = m_settings.isAway;
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        
        private void Smoke()
        {
			m_settings.isAway = false;
			awayToolStripMenuItem.Checked = false;
			string username = System.Windows.Forms.SystemInformation.UserName;
			m_client.SendRequest(username + "/smoke");
        }

		public void Smoke(SmokeClient client, string name)
		{
			if (m_settings.isAway)
				return;

			if (m_form != null)
			{
				m_form.Dispose();
			}
			m_form = new SmokinForm(m_client);
			m_form.UpdateSmoker(name, "wants to smoke");
            m_form.UpdateRequestingSmokerLabel(name + " wants to smoke!");

			m_form.Show();
		}

		public void Yes(SmokeClient client, string name)
		{
			if (m_settings.isAway)
				return;

			if (m_form != null)
			{
				m_form.UpdateSmoker(name, "says yes");
			}
		}

		public void No(SmokeClient client, string name)
		{
			if (m_settings.isAway)
				return;

			if (m_form != null)
			{
				m_form.UpdateSmoker(name, "says no");
			}
		}

		public void HoldOn(SmokeClient client, string name)
		{
			if (m_settings.isAway)
				return;

			if (m_form != null)
				m_form.UpdateSmoker(name, "says hold on");
		}

		public void Error(SmokeClient client, Exception e)
		{
			MessageBox.Show("Disconnected from server: " + e.Message, "Disconnected from server");
			Application.Exit();
			this.Close();
		}

		private SmokeSettings m_settings;
		private SmokeClient m_client;
		private SmokinForm m_form;
		private Browser m_Browser;

        private void DummyForm_Load(object sender, EventArgs e)
        {

        }
    }
}
